//手动实现线程池(固定数量)
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPoolExecutor {
    //顺序表，用于保存线程
    private List<Thread> list = new ArrayList<>();
    //阻塞队列，用于保存任务
    private BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1000);

    public MyThreadPoolExecutor(int n) {
        for (int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while(true) {
                    try {
                       Runnable runnable = blockingQueue.take();
                       runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
            list.add(t);
        }
    }

    public void submit(Runnable runnable) throws InterruptedException {
        blockingQueue.put(runnable);
    }

}
public class Test {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPoolExecutor myThreadPoolExecutor = new MyThreadPoolExecutor(4);
        for (int i = 0; i < 1000; i++) {
            int n = i;
            myThreadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("任务:" + n + " " + "线程名称:" + Thread.currentThread().getName());
                }
            });
        }

    }
}
